package qz_rpt

import java.text.SimpleDateFormat

/**
 * 石英月度一次合格率统计表
 * @Author Ivan
 * @Date 2019/4/1
 */
class OncePassYieldByMonthController {

    def commonSqlService
    def dynamicQueryService
    def exportFileService

    def index(Integer max) {
        def cellList = org.kdx.DataUtils.getCellList()
        def pageSizeList = org.kdx.DataUtils.getPageSizeList()

        [totalCount: 5, pageSize: 20, pageCurrent: 1, orderField: 'id', orderDirection: 'asc', pageSizeList: pageSizeList, cellList: cellList]
    }

    def oncePassYieldByMonth(){
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = ((pageCurrent - 1) * pageSize) + 1
        params.endno = pageCurrent * pageSize
        def oncePassYieldByMonth=[]
        def pageSizeList = [10,20,50,100]
        def totalCount =0
        def tableHeadList = []
        def oncePassYieldByMonthQty = []
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
//        def customerLotIdList = commonSqlService.customerLotIdList(params)
        params.SYS_QUERY_NAME = 'checkStepList'
        def checkStepList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'partDirect'
        def partDirect = dynamicQueryService.queryForList(params)
        if(params.EXPORT_CONTENT_NAME=='Excel'){
            params.SYS_QUERY_NAME = 'oncePassYieldByMonth'
            oncePassYieldByMonth = dynamicQueryService.queryForList(params)
            if (oncePassYieldByMonth.size() > 0) {
                tableHeadList = oncePassYieldByMonth[0].keySet()
            }
            params.SYS_QUERY_NAME = 'oncePassYieldByMonthCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT

            params.SYS_QUERY_NAME = 'oncePassYieldByMonthQty'
            oncePassYieldByMonthQty = dynamicQueryService.queryForList(params)
        }


        [pageSize  : pageSize, pageSizeList: pageSizeList, startno: params.startno ?: '', endno: params.endno ?: '',partDirect:partDirect,
         totalCount: totalCount, tableHeadList: tableHeadList,checkStepList:checkStepList,customerDirect:params.customerDirect?:'',
         group4: params.group4 ?: '', grade2: params.grade2 ?: '',partType:params.partType?:'',stepDesc:params.stepDesc?:'',
         startTime:params.startTime?:'',endTime:params.endTime?:'',defectType:params.defectType?:'',customerLotId:params.customerLotId?:'',
         oncePassYieldByMonth:oncePassYieldByMonth,oncePassYieldByMonthQty:oncePassYieldByMonthQty,
//         customerLotIdList:customerLotIdList
        ]

    }

    //一阶导出
    def exportExcel() {
        def sysQueryName


        sysQueryName = params.SYS_QUERY_NAME

        params.startno = 1
        params.endno = 99999999999999999



        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(sysQueryName=='partFinList'){
            dataList=TranposUtils.colToRow('STEP_DESC','STEP_QTY',dataList)
            println(dataList)
        }
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,startno:params.startno?:'',endno:params.endno?:''])
    }
}
